<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="sc-logo">
        <img src="../../assets/images/logo-circle.png" alt />
        <h2>云中商城管理系统</h2>
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-collapse-transition>
      <el-aside :width="isCollapse ? '64px':'200px'">
        <!-- z折叠菜单按钮 -->
        <div class="toggel-buttom">
          <i class="el-icon-s-fold" v-if="!isCollapse" @click="toggleCollapse"></i>
          <i class="el-icon-s-unfold" v-else @click="toggleCollapse"></i>
        </div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#2e4f72"
          text-color="#fff"
          active-text-color="#5dabff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id +''" v-for="item in meunlist" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              style="text-align:right"
              @click="saveNavState('/'+subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
       </el-collapse-transition>
      <!-- 主体内容区域 -->
      <el-container class="main-container">
        <el-main>
          <router-view></router-view>
        </el-main>
        <!-- 底部区域 -->
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "home",
  data() {
    return {
      //左侧菜单数据
      meunlist: [],
      iconsObj: {
        "125": "iconfont icon-users",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      //默认菜单展开
      isCollapse: false,
      //被激活的链接地址
      activePath:""
    };
  },
  created() {
    this.getMeunList();
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取所有的菜单
    async getMeunList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.meunlist = res.data;
      console.log(res);
    },
    //点击折叠菜单
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    //保存链接的激活状态
    saveNavState(activePath){
      window.sessionStorage.setItem("activePath",activePath);
      this.activePath = activePath;
    }
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  height: 95px !important;
  background-color: #2e4f72;
  color: #fff;
  text-align: center;
  line-height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sc-logo {
    display: flex;
    align-content: center;
    align-items: center;
    img {
      width: 65px;
      height: 65px;
      align-content: center;
    }
    h2 {
      font-size: 33px;
      margin-left: 15px;
    }
  }
}
.main-container {
  height: auto;
}
.el-footer {
  background-color: #2e4f72;
  color: #fff;
  height: 60px !important;
  text-align: center;
  line-height: 60px;
}
.el-aside {
  // height: 600px;
  // width: 220px !important;
  background-color: #2e4f72;
  color: #fff;
  text-align: center;
  overflow: hidden;
}

.el-main {
  background-color: rgb(255, 255, 255);
  padding: 35px;
}
.el-menu {
  border-right: none;
}
.el-menu-vertical-demo {
  height: 100%;
  overflow-y: none;
}
.iconfont {
  margin-right: 10px;
}
.toggel-buttom {
  height: 19px;
  font-size: 18px;
  text-align: right;
  cursor: pointer;
  padding-right: 25px;
}
.toggel-buttom:hover{
  color: rgb(22, 137, 231)
}
</style>